package com.imot.scheduler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.imot.config.WebSocket;
import com.imot.pojo.dto.FaultTestDto;
import com.imot.response.Result;
import com.imot.service.EmaService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * ClassName: ScheduledDatabaseQueryTask
 * Description:
 * date: 2023/11/8 13:36
 *
 * @author imot-jiah.wang
 */
@Component
@RequiredArgsConstructor
public class ScheduledDatabaseQueryTask {

    private final WebSocket webSocket;
    private final EmaService emaService;

    //获取 Ema的状态态势
    @Scheduled(fixedRate = 1000)
    private void test(){
        Result emaState = emaService.getEmaState();
        JSONObject json = (JSONObject) JSON.toJSON(emaState);
        String jsonString = json.toString();

        webSocket.sendOneMessage("getEmaState",jsonString);
    }

    //获取Ema的现在状态
    @Scheduled(fixedRate = 1000)
    public void getEmaData(){
        Result emaNow = emaService.getEmaNow();
        JSONObject json = (JSONObject) JSON.toJSON(emaNow);
        webSocket.sendOneMessage("getEmaNow",json.toString());
    }

    //获取模拟作动器的实时数据
    @Scheduled(fixedRate = 1000)
    public void getInjectFault(){
        Result result = emaService.injectFault();
        JSONObject jsonObject = (JSONObject) JSON.toJSON(result);
        webSocket.sendOneMessage("injectFault",jsonObject.toString());
    }

    //获取Ema的实时波形图
    @Scheduled(fixedRate = 1000)
    public void getEmaWaveData(){
        Result emaWaveData = emaService.getEmaWaveData();
        JSONObject json = (JSONObject) JSON.toJSON(emaWaveData);
        webSocket.sendOneMessage("getEmaWaveData",json.toString());
    }

    //获取试验时长统计
    @Scheduled(fixedRate = 1000)
    public void expDuration(){
        LocalDate end = LocalDate.now();
        LocalDate start = end.minusMonths(6);
        Result result = emaService.expDuration(start, end);
        JSONObject jsonObject = (JSONObject) JSON.toJSON(result);
        webSocket.sendOneMessage("expDuration",jsonObject.toString());
    }

    //阈值判断的获取工况情况
    @Scheduled(fixedRate = 1000)
    public void statusCheck(){
        Result result = emaService.statusCheck();
        JSONObject jsonObject = (JSONObject) JSON.toJSON(result);
        webSocket.sendOneMessage("statusCheck",jsonObject.toString());
    }

    //获取阈值判断的 运行监测
    @Scheduled(fixedRate = 1000)
    public void getRunMonitoring(){
        Result result = emaService.runMonitoring();
        JSONObject json = (JSONObject) JSON.toJSON(result);
        webSocket.sendOneMessage("runMonitoring",json.toString());
    }


    //获取描述信息
    @Scheduled(fixedRate = 1000)
    public void getDescribeFault(){
        Result result = emaService.getDescribeFault();
        JSONObject json = (JSONObject) JSON.toJSON(result);
        webSocket.sendOneMessage("getDescribeFault",json.toString());
    }


    //获取异常的实时分页数据
    @Scheduled(fixedRate = 1000)
    public void getFaultTest(){
        FaultTestDto faultTestDto = new FaultTestDto();
        faultTestDto.setPage(1);
        faultTestDto.setLimit(100);
        Result faultTest = emaService.getFaultTest(faultTestDto);
        JSONObject jsonObject = (JSONObject) JSON.toJSON(faultTest);
        webSocket.sendOneMessage("getFaultTest",jsonObject.toString());
    }

    @Scheduled(fixedRate = 1000)
    public void getFaultHistoryStatus(){
        Result result = emaService.getFaultHistoryStatus();
        JSONObject json = (JSONObject) JSON.toJSON(result);
        webSocket.sendOneMessage("getFaultHistoryStatus",json.toString());
    }
}

